//
//  TIIMQuickReplyCardView.m
//  BaseProject
//
//  Created by mac on 2020/12/7.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TIIMQuickReplyCardView.h"
#import "TIIMQuickReplyCardModel.h"
@interface TIIMQuickReplyCardCell : UITableViewCell
@property(nonatomic,strong)UILabel *titleLab;
@end

@implementation TIIMQuickReplyCardCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = WhiteColor;
        self.contentView.backgroundColor = WhiteColor;
    }
    return self;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = rgba(255, 66, 66, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(5*ADAPTER_WIDTH, 6*ADAPTER_WIDTH, 290*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
        _titleLab = lab;
    }
    return _titleLab;
}
@end


@interface TIIMQuickReplyCardView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)BaseAdjustButton *flexButton;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)UITableView *mainView;
@end

@implementation TIIMQuickReplyCardView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    self.image = [UIImage getPNGimageInBundleWithName:@"Chat_quick_card"];
    self.userInteractionEnabled = YES;
}
- (void)createUI
{
    self.titleLab.text = @"您是遇到以下问题吗？请点击提问";
    [self lineView];
    [self flexButton];
    [self mainView];
}
#pragma mark - setter
- (void)setCardModel:(TIIMQuickReplyCardModel *)cardModel
{
    _cardModel = cardModel;
    [self.mainView reloadData];
}
#pragma mark - click
- (void)clickFlexButton:sender
{
    !self.clickFlexButtonBlock ?: self.clickFlexButtonBlock ();
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cardModel.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TIIMQuickReplyCardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLab.text = self.cardModel.data[indexPath.row].content;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    !self.callBackContentBlock ?: self.callBackContentBlock (self.cardModel.data[indexPath.row]);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 32*ADAPTER_WIDTH;
}
#pragma mark - lazy懒加载
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = rgba(34, 34, 34, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(16*ADAPTER_WIDTH, 13*ADAPTER_WIDTH, self.width - 32*ADAPTER_WIDTH, 17*ADAPTER_WIDTH);
        _titleLab = lab;
    }
    return _titleLab;
}
- (BaseAdjustButton *)flexButton
{
    if (!_flexButton) {
        BaseAdjustButton *button = [BaseAdjustButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        button.type = layoutTypeHorizontalLeftTitleRightImage;
        button.title = @"点击收起问题";
        button.fontWeight = UIFontWeightRegular;
        button.fontSize = 14*ADAPTER_WIDTH;
        button.imageWidth = 10*ADAPTER_WIDTH;
        button.imageHeight = 11*ADAPTER_WIDTH;
        button.marginLeft = 17*ADAPTER_WIDTH;
        button.marginItemX = 6*ADAPTER_WIDTH;
        button.marginRight = 17*ADAPTER_WIDTH;
        button.marginTop = 15*ADAPTER_WIDTH;
        button.marginBottom = 15*ADAPTER_WIDTH;
        [button begin];
        [button setTitleColor:rgba(102, 102, 102, 1) forState:UIControlStateNormal];
        [button setImage:[UIImage getPNGimageInBundleWithName:@"Chat_quick_flex"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickFlexButton:) forControlEvents:UIControlEventTouchUpInside];
        button.left = 0;
        button.bottom = self.height - 12*ADAPTER_WIDTH;
        _flexButton = button;
    }
    return _flexButton;
}
- (UIView *)lineView
{
    if (!_lineView) {
        UIView *view = [[UIView alloc]init];
        [self addSubview:view];
        view.backgroundColor = kLineColor;
        view.frame = CGRectMake(4*ADAPTER_WIDTH, 40*ADAPTER_WIDTH, self.width - 8*ADAPTER_WIDTH, kLineHeight);
        _lineView = view;
    }
    return _lineView;
}
- (UITableView *)mainView
{
    if (!_mainView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(5*ADAPTER_WIDTH, self.lineView.bottom + 5*ADAPTER_WIDTH, self.width - 10*ADAPTER_WIDTH, self.flexButton.top - self.lineView.bottom) style:UITableViewStylePlain];
        [self addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        [tableView registerClass:[TIIMQuickReplyCardCell class] forCellReuseIdentifier:@"Cell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = [UIColor whiteColor];
        if (@available(iOS 11.0, *))
        {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            tableView.estimatedRowHeight = 0;
            tableView.estimatedSectionFooterHeight = 0;
            tableView.estimatedSectionHeaderHeight = 0;
        }
        else
        {
            //self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _mainView = tableView;
    }
    return _mainView;
}
@end
